package com.ocean.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocean.admin.entity.SysConfig;
import com.ocean.admin.mapper.SysConfigMapper;
import com.ocean.admin.model.dto.config.SysConfigDTO;
import com.ocean.admin.model.dto.config.SysConfigSaveDTO;
import com.ocean.admin.model.dto.config.SysConfigUpdateDTO;
import com.ocean.admin.model.vo.config.SysConfigVo;
import com.ocean.admin.extension.security.core.SysUserDetails;
import com.ocean.admin.extension.security.filter.WebFilterExtend;
import com.ocean.admin.service.ISysConfigService;
import com.ocean.core.util.constants.ConstantsAttribute;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 参数配置表 服务实现类
 * </p>
 *
 * @author zhang
 * @since 2022-08-16
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements ISysConfigService {

    /**
     * 新增参数配置
     *
     * @param saveDTO 参数信息
     * @return 新增结果
     */
    @Override
    public String saveConfig(SysConfigSaveDTO saveDTO) {
        SysConfig config = new SysConfig();
        BeanUtils.copyProperties(saveDTO, config);
        SysUserDetails current = WebFilterExtend.current();
        config.setCreateUserId(current.getUserId());
        config.setCreateUserName(current.getUsername());
        config.setUpdateUserId(current.getUserId());
        config.setUpdateUserName(current.getNickName());
        this.save(config);
        return ConstantsAttribute.INSERT_SUCCESS;
    }

    /**
     * 删除参数配置
     *
     * @param configIds 参数Id
     * @return 结果
     */
    @Override
    public String removeConfig(Long[] configIds) {
        List<Long> longs = Arrays.asList(configIds);
        this.removeByIds(longs);
        return ConstantsAttribute.REMOVE_SUCCESS;
    }

    /**
     * 修改参数配置
     *
     * @param updateDTO 参数信息
     * @return 修改结果
     */
    @Override
    public String updateConfig(SysConfigUpdateDTO updateDTO) {
        SysConfig config = new SysConfig();
        BeanUtils.copyProperties(updateDTO, config);
        SysUserDetails current = WebFilterExtend.current();
        config.setUpdateUserId(current.getUserId());
        config.setUpdateUserName(current.getNickName());
        this.save(config);
        return ConstantsAttribute.UPDATE_SUCCESS;
    }

    /**
     * 获取参数配置列表
     *
     * @param configDTO 查询条件
     * @return 分页结果
     */
    @Override
    public IPage<SysConfigVo> pageList(SysConfigDTO configDTO) {
        Page<SysConfigVo> page = new Page<>(configDTO.getPage(), configDTO.getPageSize());
        IPage<SysConfigVo> pageList = baseMapper.selectPageList(page, configDTO);
        pageList.getRecords().forEach(res -> res.setConfigTypeName(res.getConfigType() == 1 ? "是" : "否"));
        return pageList;
    }
}
